﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsEmploymentType
    {
        public string EmploymentType { get; set; }
        public int EmploymentTypeID { get; set; }

        public DataTable GetAllEmployments()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from EmplyomentType", "EmplyomentType");
            }
        }
        public void GetEmployment(int employementTypeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from EmplyomentType where EmploymentTypeID=" + employementTypeID.ToString(), "EmplymentType");
                if (dt.Rows.Count > 0)
                {
                    EmploymentTypeID = Convert.ToInt32(dt.Rows[0]["EmploymentTypeID"]);
                    EmploymentType = dt.Rows[0]["EmploymentType"].ToString();
                }
            }
        }
        public bool SaveEmployment(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from EmplyomentType where EmploymentType='" + EmploymentType + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Employment Type already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into EmplyomentType (EmploymentType) values('" + EmploymentType + "')");
                    }
                }
                else
                {
                    string count = data.GetValue("select count(*) from EmplyomentType where EmploymentType='" + EmploymentType + "' and EmploymentTypeID NOT IN (" + EmploymentTypeID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Employment Type already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update EmplyomentType set EmploymentType = '" + EmploymentType + "' where EmploymentTypeID=" + EmploymentTypeID.ToString());
                    }

                }
            }
            return true;
        }
        public void DeleteEmployment()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  EmplyomentType where EmploymentTypeID=" + EmploymentTypeID.ToString());
            }
        }
    }



}
